package com.luge.heart;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView.RecyclerListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luge.base.Base;
import com.luge.base.BaseNetActivity;
import com.luge.model.KeyItem;
import com.luge.model.KeyList;
import com.luge.model.KeyListAdapter;
import com.luge.model.SongDetails;
import com.luge.model.SongItem;
import com.luge.model.SongList;
import com.luge.model.SongListAdapter;
import com.luge.model.download.CallBackImageDown;
import com.luge.model.request.Task;
import com.luge.model.view.MusicLayout;
import com.luge.model.view.ReleaseInterface;
import com.luge.utils.Helper;
import com.luge.utils.SongPlayer;

public class MainActivity extends BaseNetActivity implements
		OnCheckedChangeListener, OnClickListener, OnItemClickListener,
		RecyclerListener {
	private static final int SINGLE = 1;
	private static final int PURSUE = 2;
	private static final int LOVE = 3;
	private static final int LOSE_LOVE = 4;
	private static final int ELSE = 5;

	private RadioGroup radioGroup;
	private RadioButton singleBtn;
	private RadioButton pursueBtn;
	private RadioButton loveBtn;
	private RadioButton loseLoveBtn;
	private RadioButton elseBtn;

	private ImageView logoImageView;
	private GridView keyGridView;
	private ImageView keyImageView;

	private RelativeLayout songListLayout;
	private GridView songGridView;
	private Button closeBtn;
	private TextView keyTxt;

	private MusicLayout musicLayout;

	private ArrayList<KeyItem> singleList;
	private ArrayList<KeyItem> pursueList;
	private ArrayList<KeyItem> loveList;
	private ArrayList<KeyItem> loseLoveList;
	private ArrayList<KeyItem> elseList;

	private KeyListAdapter keyListAdapter;
	private SongListAdapter songListAdapter;

	private ArrayList<SongItem> songs;
	private SongDetails songDetails;
	private int tabType;
	private String keyName;
	public static String songId;
	private SongItem songItem;

	@Override
	protected void init() {
		setContentView(R.layout.main);
		keyGridView = (GridView) findViewById(R.id.main_gridview_key);
		keyGridView.setOnItemClickListener(this);
		keyGridView.setRecyclerListener(this);
		songListLayout = (RelativeLayout) findViewById(R.id.songlist_layout);
		songGridView = (GridView) findViewById(R.id.main_gridview_song);
		songGridView.setOnItemClickListener(this);
		songGridView.setRecyclerListener(this);

		radioGroup = (RadioGroup) findViewById(R.id.main_radio_group);
		radioGroup.setOnCheckedChangeListener(this);
		singleBtn = (RadioButton) findViewById(R.id.main_single_btn);
		pursueBtn = (RadioButton) findViewById(R.id.main_pursue_btn);
		loveBtn = (RadioButton) findViewById(R.id.main_love_btn);
		loseLoveBtn = (RadioButton) findViewById(R.id.main_lose_love_btn);
		elseBtn = (RadioButton) findViewById(R.id.main_else_btn);

		musicLayout = (MusicLayout) findViewById(R.id.main_music_layout);
		closeBtn = (Button) findViewById(R.id.songlist_close);
		closeBtn.setOnClickListener(this);

		keyTxt = (TextView) findViewById(R.id.key_txt);
		keyImageView = (ImageView) findViewById(R.id.main_key_bg);
		keyImageView.getLayoutParams().height = (int) (Helper
				.getDisplayWidth(this) / 1.67);
		logoImageView = (ImageView) findViewById(R.id.main_logo);
		logoImageView.getLayoutParams().height = (int) (Helper
				.getDisplayWidth(this) / 1.67);
	}

	@Override
	protected void main() {
		tabType = SINGLE;
		request(Task.KEY_LIST);
	}

	@Override
	protected String getUrl(int taskID) {
		switch (taskID) {
		case Task.KEY_LIST:
			return String.format(getString(R.string.getSongKeys), tabType,
					Base.key);
		case Task.SONG_LIST:
			return String.format(getString(R.string.getSongList), tabType,
					Helper.utf_8(keyName), Base.key);
		case Task.SONG_DETAILS:
			return String.format(getString(R.string.getSongDetails), songId,
					Base.key);
		default:
			break;
		}
		return null;
	}

	@Override
	public boolean handleMessage(Message arg0) {
		switch (arg0.what) {
		case Task.KEY_LIST:
			KeyList model = (KeyList) arg0.obj;
			if (preResponse(this, model)) {
				switch (tabType) {
				case SINGLE:
					singleList = model.getList();
					keyListAdapter = new KeyListAdapter(singleList);
					break;
				case PURSUE:
					pursueList = model.getList();
					keyListAdapter = new KeyListAdapter(pursueList);
					break;
				case LOVE:
					loveList = model.getList();
					keyListAdapter = new KeyListAdapter(loveList);
					break;
				case LOSE_LOVE:
					loseLoveList = model.getList();
					keyListAdapter = new KeyListAdapter(loseLoveList);
					break;
				case ELSE:
					elseList = model.getList();
					keyListAdapter = new KeyListAdapter(elseList);
					break;
				default:
					break;
				}
				keyGridView.setAdapter(keyListAdapter);
			}
			break;
		case Task.SONG_LIST:
			SongList songList = (SongList) arg0.obj;
			if (preResponse(this, songList)) {
				songListLayout.setVisibility(View.VISIBLE);
				keyImageView.setVisibility(View.VISIBLE);
				musicLayout.setVisibility(View.GONE);
				keyGridView.setVisibility(View.GONE);
				keyTxt.setText(getString(R.string.point) + " " + keyName + " "
						+ getString(R.string.point));
				logoImageView.setVisibility(View.GONE);
				songs = songList.getList();
				songListAdapter = new SongListAdapter(songs);
				songGridView.setAdapter(songListAdapter);
				new CallBackImageDown(songList.getBigImageUrl(),
						new CallBackImageDown.CallBackInterface() {

							@Override
							public void success(Bitmap bitmap) {
								keyImageView.setImageBitmap(bitmap);
							}

							@Override
							public void fail() {
								keyImageView
										.setImageResource(R.drawable.key_background);
							}
						});
			}
			break;
		case Task.SONG_DETAILS:
			songDetails = (SongDetails) arg0.obj;
			if (preResponse(this, songDetails)) {
				keyImageView.setVisibility(View.GONE);
				musicLayout.setVisibility(View.VISIBLE);
				musicLayout.setInfo(songDetails, songItem.getName(),
						songItem.getSinger());
				songListAdapter.notifyDataSetChanged();
			}
			break;
		default:
			break;
		}
		return false;
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		songListLayout.setVisibility(View.GONE);
		musicLayout.setVisibility(View.GONE);
		musicLayout.stop();
		keyGridView.setVisibility(View.VISIBLE);
		keyImageView.setVisibility(View.GONE);
		logoImageView.setVisibility(View.VISIBLE);
		songId = null;
		switch (checkedId) {
		case R.id.main_single_btn:
			tabType = SINGLE;
			singleBtn.setChecked(true);
			if (singleList == null) {
				request(Task.KEY_LIST);
			} else {
				keyListAdapter = new KeyListAdapter(singleList);
				keyGridView.setAdapter(keyListAdapter);
			}
			break;
		case R.id.main_pursue_btn:
			pursueBtn.setChecked(true);
			tabType = PURSUE;
			if (pursueList == null) {
				request(Task.KEY_LIST);
			} else {
				keyListAdapter = new KeyListAdapter(pursueList);
				keyGridView.setAdapter(keyListAdapter);
			}
			break;
		case R.id.main_love_btn:
			loveBtn.setChecked(true);
			tabType = LOVE;
			if (loveList == null) {
				request(Task.KEY_LIST);
			} else {
				keyListAdapter = new KeyListAdapter(loveList);
				keyGridView.setAdapter(keyListAdapter);
			}
			break;
		case R.id.main_lose_love_btn:
			loseLoveBtn.setChecked(true);
			tabType = LOSE_LOVE;
			if (loseLoveList == null) {
				request(Task.KEY_LIST);
			} else {
				keyListAdapter = new KeyListAdapter(loseLoveList);
				keyGridView.setAdapter(keyListAdapter);
			}
			break;
		case R.id.main_else_btn:
			elseBtn.setChecked(true);
			tabType = ELSE;
			if (elseList == null) {
				request(Task.KEY_LIST);
			} else {
				keyListAdapter = new KeyListAdapter(elseList);
				keyGridView.setAdapter(keyListAdapter);
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.songlist_close) {
			songListLayout.setVisibility(View.GONE);
			keyGridView.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (arg0.getId() == R.id.main_gridview_key) {
			ArrayList<KeyItem> keys = getKeys(tabType);
			KeyItem keyItem = keys.get(arg2);
			keyName = keyItem.getTitle();
			request(Task.SONG_LIST);
		} else if (arg0.getId() == R.id.main_gridview_song) {
			songItem = songs.get(arg2);
			songId = songItem.getId();
			request(Task.SONG_DETAILS);
		}
	}

	private ArrayList<KeyItem> getKeys(int tabType2) {
		switch (tabType2) {
		case SINGLE:
			return singleList;
		case PURSUE:
			return pursueList;
		case LOVE:
			return loveList;
		case LOSE_LOVE:
			return loseLoveList;
		case ELSE:
			return elseList;
		default:
			break;
		}
		return null;
	}

	@Override
	public void onMovedToScrapHeap(View view) {
		ReleaseInterface releaseInterface = (ReleaseInterface) view;
		releaseInterface.release();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

			new AlertDialog.Builder(this)
					.setMessage(getString(R.string.exit_sure))
					.setPositiveButton(R.string.sure_str,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									SongPlayer.getInstance().stop();
									finish();
								}
							})
					.setNegativeButton(R.string.clear_str,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
								}
							}).show();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
